﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Wyse.Server.Logging
{
    /// <summary>
    /// File-based log listener
    /// </summary>
    public class FileLogListener : ILogListener, IDisposable
    {
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="filename">logfile name</param>
        /// <param name="append">append or truncate?</param>
        public FileLogListener(String filename, bool append = true)
        {
            if (append)
            {
                mWriter = File.AppendText(filename);
                mWriter.WriteLine();
            }
            else
                mWriter = File.CreateText(filename);
        }

        public void LogMessage(string message)
        {
            mWriter.WriteLine(message);
            mWriter.Flush();
        }

        public void Dispose()
        {
            if (mWriter != null)
            {
                mWriter.Flush();
                mWriter.Close();
            }
        }

        private StreamWriter mWriter;
    }
}
